home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / grafica / amhelios / ff_scan.cpp < prev    next >
C/C++ Source or Header  |  1999-01-01  |  5KB  |  159 lines

  1. ////////////////////////////////////////////////////////////
  2. //
  3. //  FF_SCAN.CPP - Form Factor Scan Conversion Class
  4. //
  5. //  Version:    1.03A
  6. //
  7. //  History:    94/08/23 - Version 1.00A release.
  8. //              94/12/16 - Version 1.01A release.
  9. //              95/02/05 - Version 1.02A release.
  10. //              95/07/21 - Version 1.02B release.
  11. //              96/02/14 - Version 1.02C release.
  12. //              96/04/01 - Version 1.03A release.
  13. //
  14. //  Compilers:  Microsoft Visual C/C++ Professional V1.5
  15. //              Borland C++ Version 4.5
  16. //
  17. //  Author:     Ian Ashdown, P.Eng.
  18. //              byHeart Software Limited
  19. //              620 Ballantree Road
  20. //              West Vancouver, B.C.
  21. //              Canada V7S 1W3
  22. //              Tel. (604) 922-6148
  23. //              Fax. (604) 987-7621
  24. //
  25. //  Copyright 1994-1996 byHeart Software Limited
  26. //
  27. //  The following source code has been derived from:
  28. //
  29. //    Ashdown, I. 1994. Radiosity: A Programmer's
  30. //    Perspective. New York, NY: John Wiley & Sons.
  31. //
  32. //  It may be freely copied, redistributed, and/or modified
  33. //  for personal use ONLY, as long as the copyright notice
  34. //  is included with all source code files.
  35. //
  36. ////////////////////////////////////////////////////////////
  37.  
  38. #include "ff_delta.h"
  39. #include "ff_scan.h"
  40.  
  41. // Scan convert polygon
  42. void FormScan::Scan( FormPoly &poly )
  43. {
  44.   poly_id = poly.GetPolyId();   // Get polygon identifier
  45.   GetVertexInfo(poly);          // Get vertex information
  46.   ScanEdges();                  // Scan convert edges
  47.   DrawEdgeList();               // Draw edge list
  48. }
  49.  
  50. // Get vertex information
  51. void FormScan::GetVertexInfo( FormPoly &poly )
  52. {
  53.   int i;                // Loop index
  54.   FormVertexInfo *pv;   // Vertex info element pointer
  55.   Point3 posn;          // Normalized vertex position
  56.  
  57.   // Initialize polygon y-axis limits
  58.   ymax = 0;
  59.   ymin = FF_ArrayRes - 1;
  60.  
  61.   // Get number of vertices
  62.   num_vert = poly.GetNumVert();
  63.  
  64.   for (i = 0; i < num_vert; i++)
  65.   {
  66.     pv = &(v_info[i]);  // Get vertex info element pointer
  67.  
  68.     // Get vertex normalized view space co-ordinates
  69.     posn = poly.GetVertex(i);
  70.  
  71.     // Scale view space u-v co-ordinates
  72.     pv->posn.SetX(posn.GetX() * FF_ArrayRes);
  73.     pv->posn.SetY(posn.GetY() * FF_ArrayRes);
  74.     pv->posn.SetZ(posn.GetZ());
  75.  
  76.     // Convert to cell array x-y co-ordinates
  77.     pv->face.x = (int) pv->posn.GetX();
  78.     pv->face.y = (int) pv->posn.GetY();
  79.  
  80.     // Update polygon y-axis limits
  81.     if (pv->face.y < ymin)
  82.       ymin = pv->face.y;
  83.     if (pv->face.y > ymax)
  84.       ymax = pv->face.y;
  85.   }
  86. }
  87.  
  88. void FormScan::ScanEdges()      // Scan convert edges
  89. {
  90.   int i, j;             // Loop indices
  91.   double dx;            // X-axis delta
  92.   double dz;            // Pseudodepth delta
  93.   double ix;            // Intersection X-axis co-ordinate
  94.   double iz;            // Intersection pseudodepth
  95.   double y_dist;        // Y-axis distance
  96.   FormEdgeInfo *pedge;  // Edge info pointer
  97.   FormScanInfo *pscan;  // Scan line info pointer
  98.   FormVertexInfo *psv;  // Start vertex info pointer
  99.   FormVertexInfo *pev;  // End vertex info pointer
  100.   FormVertexInfo *psw;  // Swap vertex info pointer
  101.  
  102.   // Initialize edge list
  103.   for (i = ymin; i < ymax; i++)
  104.     edge_list[i].first = FALSE;
  105.  
  106.   for (i = 0; i < num_vert; i++)
  107.   {
  108.     // Get edge vertex pointers
  109.     psv = &(v_info[i]);
  110.     pev = &(v_info[(i + 1) % num_vert]);
  111.     
  112.     if (psv->face.y == pev->face.y)
  113.     {
  114.       continue;         // Ignore horizontal edges
  115.     }
  116.  
  117.     if (psv->face.y > pev->face.y)
  118.     {
  119.       // Swap edge vertex pointers
  120.       psw = psv; psv = pev; pev = psw;
  121.     }
  122.  
  123.     // Get start vertex info
  124.     ix = psv->posn.GetX();
  125.     iz = psv->posn.GetZ();
  126.  
  127.     // Determine inverse slopes
  128.     y_dist = (double) (pev->face.y - psv->face.y);
  129.  
  130.     dx = (pev->posn.GetX() - ix) / y_dist;
  131.     dz = (pev->posn.GetZ() - iz) / y_dist;
  132.  
  133.     // Scan convert edge
  134.     pedge = &(edge_list[psv->face.y]);
  135.     for (j = psv->face.y; j < pev->face.y; j++)
  136.     {
  137.       // Determine intersection info element
  138.       if (pedge->first == FALSE)
  139.       {
  140.         pscan = &(pedge->isect[0]);
  141.         pedge->first = TRUE;
  142.       }
  143.       else
  144.         pscan = &(pedge->isect[1]);
  145.  
  146.       // Insert edge intersection info
  147.       pscan->x = ix;
  148.       pscan->z = iz;
  149.  
  150.       // Update edge intersection info
  151.       ix += dx;
  152.       iz += dz;
  153.  
  154.       pedge++;  // Point to next edge list element
  155.     }
  156.   }
  157. }
  158.  
  159.